package eva.savant.business.ventas.view

import eva.savant.business.ventas.service.TSVenta
import eva.savant.businessobject.vt.Venta

import javax.faces.bean.ManagedBean
import javax.faces.bean.SessionScoped
import javax.annotation.PostConstruct

@SessionScoped
@ManagedBean(name = 'ITVentaAnular')
public class ITVentaAnular extends Bean {

  Integer ventaId

  Date dateIni, dateEnd

  @PostConstruct
  public void cleanEntity() {

    ventaId = 0

    entity  = new Venta()

    dateIni = dateEnd= new Date()
  }

  public void editEntity(Object id) {

    entity= get(Venta.class,id)
  }

  public List findLsEntity() {

    if(ventaId > 0) return find(Venta.class,'id=?',ventaId)

    return find(Venta.class,'fecha between ? and ? order by id',toStartDay(dateIni),toMidnight(dateEnd))
  }

  public boolean mergeEntity(Object entity) { entity= (Venta) entity

    def tsVenta= (TSVenta) getSpringBean('tsVenta'); tsVenta.anularVenta(entity)

    addInfoMessage 'Se anulo correctamente la VENTA ' + entity?.id; return entity != null
  }

}